//
//  CCommon.m
//  KurtulusLaw
//
//  Created by phuc phuc on 12/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CCommon.h"
#import "Constant.h"
@implementation CCommon

static NSString *_sessionID;
static NSString *_username;
static NSString *_password;
static NSString *_userID;

+ (NSString *)shareUsername {
	if (!_username) {
		_username = @"";
	}
	
	return _username;
}
+ (void)setUsername:(NSString *)username {
	_username = [username copy];
}
+ (NSString *)sharePassword {
	if (!_password) {
		_password = @"";
	}
	
	return _password;
}

+(void)setPasword:(NSString *)password {
	_password = [password copy];
}

+ (NSString *)shareSessionID {
    if (!_sessionID) {
        _sessionID = @"";
    }
    
    return _sessionID;
}

+ (void)setSesionID:(NSString *) sessionID {
	_sessionID = [sessionID copy];
}
+ (void) resetPasscode
{
	NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
	[userDefault removeObjectForKey:kCode1];
	[userDefault removeObjectForKey: kCode2];
	[userDefault removeObjectForKey: kCode3];
	[userDefault removeObjectForKey: kCode4];
	[userDefault synchronize];
	
}
+ (void)deleteUserDirectory:(NSString *)userID {
	NSError *error;
	//make a file name to write the data to using the documents directory:
	NSString *filePath = [CCommon getUserDirectory];
	
	[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
	
}
+ (void) resetDataWhenLogout
{
	NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
	[userDefault removeObjectForKey:kKEY_USERNAME];
	[userDefault removeObjectForKey: kKEY_PASSWORD];
	
	[userDefault synchronize];
	[CCommon resetPasscode];
	NSString *userID = [CCommon shareUserID];
	
	if ([userID length] != 0) {
		[self deleteUserDirectory:userID];
	}
	
	NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
	[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
	
}
+(void)resetuserDefault
{
    NSDictionary * allObjects;
    NSString     * key;
    
    allObjects = [ [ NSUserDefaults standardUserDefaults ] dictionaryRepresentation ];
    
    for( key in allObjects )
    {
        [ [ NSUserDefaults standardUserDefaults ] removeObjectForKey: key ];
    }
    
    [ [ NSUserDefaults standardUserDefaults ] synchronize ];
}
+(void)createPasscode : (NSString*)_code1 : (NSString*)_code2 : (NSString*)_code3 : (NSString*)_code4 
{
    NSUserDefaults *userDefauls = [NSUserDefaults standardUserDefaults];
    [userDefauls setObject:_code1 forKey: kCode1];
    [userDefauls setObject:_code2 forKey: kCode2];
    [userDefauls setObject:_code3 forKey: kCode3];
    [userDefauls setObject:_code4 forKey: kCode4];
    [userDefauls synchronize];
}
#pragma mark - checkPasscode
+(BOOL)checkPasscode
{
    NSUserDefaults *userDefauls = [NSUserDefaults standardUserDefaults];
    NSString *passCode = [userDefauls objectForKey:kCode1];
    if (passCode) {
        return YES;
    }
    else
        return NO;
    
}

+(void)callToKurtulus
{
    NSString *phoneStr = [NSString stringWithFormat:@"tel:%@",kPhoneHasanKurtulus];
    NSString *escaped = [phoneStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    //detect device (iPhone | iPad | IPod)
    NSString *device = [UIDevice currentDevice].model;
    if ([device hasPrefix:@"iPhone"]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
    } else if ([device hasPrefix:@"iPod"] || [device hasPrefix:@"iPad"]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kWebKurtulus]];
    }
	
}

+ (NSString *)shareUserID {
	if (!_userID) {
		_userID = @"";
	}
	
	return _userID;
}
+ (void)setUserID:(NSString *)userID {
	_userID = [userID copy];
}

+(NSString *)getUserDirectory {
	NSArray *paths = NSSearchPathForDirectoriesInDomains
	(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	//make a file name to write the data to using the documents directory:
	NSString *filePath = [NSString stringWithFormat:@"%@/%@", 
						  documentsDirectory, [CCommon shareUserID]];
	
	return  filePath;

}
@end
